#ifndef PACKETBASE_H
#define PACKETBASE_H

#include <QString>

#define BYTE unsigned char
#define SHORT unsigned short
#define UINT unsigned int


class PacketBase
{
public:
    PacketBase();

    const static unsigned short DEPTH_UNSET = 65535;
    // 报文数据长度为19字节
    const static int PACKET_LENGTH = 19;

    // 经纬度的精度
    const static int BASE_METRIC = 10000000;

    // 经度的范围
    enum LONGITUDE_RANGE {
        LONGITUE_MIN = 0,
        LONGITUE_MAX = 180*BASE_METRIC
    };

    enum LATITUDE_RANGE {
        LATITUDE_MIN = 0,
        LATITUDE_MAX = 90*BASE_METRIC
    };

    // 深度的范围设为0.00米~655.35米
    enum DEPTH_RANGE {
        DEPTH_MIN = 0,
        DEPTH_MAX = 65534
    };

    enum BYTE_LENGTH {
        HEAD_LENGTH = 2,
        ID_LENGTH = 2,
        LONGITUE_LENGTH = 4,
        LATITUDE_LENGTH =4,
        DEPTH_LENGTH = 2,
        TEMPERATURE_LENGTH = 4,
        CHECKSUM_LENGTH = 1
    };

    QString decode(QByteArray rawData = 0);
    QString encode();

    void setCoordinate(unsigned int longitude, unsigned int latitude);
    void setDepth(SHORT depth);

    void resetData();

protected:

    BYTE rawData[PACKET_LENGTH];

        // 报文头默认为0xAC 0xBC
    BYTE head[2];

       // 目标号 1~5为蛙人, 6为ROV
    SHORT id;

       // 目标经度, 精度为0.0000001, 例如1101234567表示110.1234567
    unsigned int longitude;

        // 目标纬度, 精度0.0000001, 例如171234567表示17.1234567
    unsigned int latitude;

        // 目标深度, 精度0.01m, 例如424表示4.24米
    SHORT depth;

    // ROV当前位置的水温, 精度为0.0001度, 例如10012表示1.0012度
    unsigned int temperature;

        //校验和, 默认为0x00
    BYTE checksum;

private:
    bool isValidCoord(UINT longitude, UINT latitude);
    bool isValidDepth(SHORT depth);

    QString toHexString();
};

#endif // PACKETBASE_H
